#!/bin/sh

TEST_DIR=../src/edu/mit/media/funf
if [[ ! -d $TEST_DIR ]]; then
	echo "Source directory $TEST_DIR not found."
	echo "This script has to be run from <checkout>/docs"
	exit 1
fi

# Commandline parsing taken from
# http://blog.mafr.de/2007/08/05/cmdline-options-in-shell-scripts/

USAGE="Usage: `basename $0` [-h] -l linked_url -p package_url -c classpath_jar"
LOCAL=.build.local

# Try to parse parameters from local file if available.
if [[ -f $LOCAL ]]; then
	source $LOCAL
fi

# Parse command line options.
while getopts hc:p:l: OPT; do
    case "$OPT" in
        h)
            echo $USAGE
            exit 0
            ;;
		l)
			LINKED_URL=$OPTARG
			;;
		p)
			PACKAGE_URL=$OPTARG
			;;
        c)
            CLASSPATH_JAR=$OPTARG
            ;;
        \?)
            # getopts issues an error message
            echo $USAGE >&2
            exit 1
            ;;
    esac
done

if [[ -z $LINKED_URL ]]; then
	echo "Pass the location of the linked android documentation (-l)."
	echo "Usually you want to pass the URL http://d.android.com/reference"
	echo $USAGE
	exit 1
fi

if [[ -z $PACKAGE_URL ]]; then
	echo "Pass the package url for the android documentation (-p)."
	echo "Usually this is \$ANDROID_HOME/docs/reference"
	echo $USAGE
	exit 1
fi

if [[ -z $CLASSPATH_JAR ]]; then
	echo "Pass the location of the android classpath jar file (-c)."
	echo "Usually this is \$ANDROID_HOME/platforms/android-<number>/android.jar"
	echo $USAGE
	exit 1
fi

echo "Building docs linking to $LINKED_URL"
echo "Local reference set to $PACKAGE_URL"
echo "Local android classpath set to $CLASSPATH_JAR"
echo "Invoking javadoc -quiet -sourcepath ../src -subpackages edu -d output -linkoffline $LINKED_URL $PACKAGE_URL -classpath $CLASSPATH_JAR"

javadoc -quiet -sourcepath ../src -subpackages edu -d output \
	-linkoffline $LINKED_URL $PACKAGE_URL -classpath $CLASSPATH_JAR
